package display;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import quantity.Quantity;
import quantity.QuantityList;
import Util.Util;
import book.Book;
import book.Edition;

import javax.swing.JButton;

import exception.InvalidEditionException;

import javax.swing.JComboBox;

public class BookInfo extends JFrame {

	private JPanel contentPane;
	private JTextField invNumField;
	private JScrollPane qtyScrollPane, adoptScrollPane;
	private JTable qtyTable, adoptTable;
	private JLabel lblQuantities, lblAdoptions, lblAuthor, lblTitle;
	private JComboBox jcbEd;
	
	private InventoryData inv;
	private JButton btnMassUpdateFrom;

	/**
	 * Create the frame.
	 */
	public BookInfo(InventoryData in) {
		inv = in;
		initDisplay();
		initLabels();
		initTables();
		initButtons();
	}
	
	private void initDisplay() {
		
		setTitle("Title Information Display");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 30, 30, 30, 30, 30, 30, 0, 30, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0};
		contentPane.setLayout(gbl_contentPane);
		
	}
	
	private void initLabels() {
		
		lblTitle = new JLabel("Title");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 20));
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridheight = 2;
		gbc_lblTitle.gridwidth = 10;
		gbc_lblTitle.anchor = GridBagConstraints.WEST;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 0;
		contentPane.add(lblTitle, gbc_lblTitle);
		
		lblAuthor = new JLabel("Author");
		lblAuthor.setFont(new Font("Tahoma", Font.PLAIN, 20));
		GridBagConstraints gbc_lblAuthor = new GridBagConstraints();
		gbc_lblAuthor.anchor = GridBagConstraints.WEST;
		gbc_lblAuthor.gridwidth = 4;
		gbc_lblAuthor.insets = new Insets(0, 0, 5, 5);
		gbc_lblAuthor.gridx = 1;
		gbc_lblAuthor.gridy = 2;
		contentPane.add(lblAuthor, gbc_lblAuthor);
		
		jcbEd = new JComboBox();
		jcbEd.setFont(new Font("Tahoma", Font.PLAIN, 20));
		GridBagConstraints gbc_lblEd = new GridBagConstraints();
		gbc_lblEd.gridwidth = 4;
		gbc_lblEd.insets = new Insets(0, 0, 5, 5);
		gbc_lblEd.gridx = 9;
		gbc_lblEd.gridy = 2;
		contentPane.add(jcbEd, gbc_lblEd);
		
		JLabel lblIsbn = new JLabel("ISBN");
		GridBagConstraints gbc_lblIsbn = new GridBagConstraints();
		gbc_lblIsbn.insets = new Insets(0, 0, 5, 5);
		gbc_lblIsbn.anchor = GridBagConstraints.EAST;
		gbc_lblIsbn.gridx = 1;
		gbc_lblIsbn.gridy = 3;
		contentPane.add(lblIsbn, gbc_lblIsbn);
		
		invNumField = new JTextField();
		invNumField.addKeyListener(new InvKeyListener());
		GridBagConstraints gbc_isbnField = new GridBagConstraints();
		gbc_isbnField.gridwidth = 5;
		gbc_isbnField.insets = new Insets(0, 0, 5, 5);
		gbc_isbnField.fill = GridBagConstraints.HORIZONTAL;
		gbc_isbnField.gridx = 2;
		gbc_isbnField.gridy = 3;
		contentPane.add(invNumField, gbc_isbnField);
		invNumField.setColumns(10);
		
		lblAdoptions = new JLabel("Adoptions");
		GridBagConstraints gbc_lblAdoptions = new GridBagConstraints();
		gbc_lblAdoptions.gridwidth = 11;
		gbc_lblAdoptions.insets = new Insets(0, 0, 5, 5);
		gbc_lblAdoptions.gridx = 1;
		gbc_lblAdoptions.gridy = 4;
		gbc_lblAdoptions.gridheight = 1;
		contentPane.add(lblAdoptions, gbc_lblAdoptions);
		
		lblQuantities = new JLabel("Quantities");
		GridBagConstraints gbc_lblQuantities = new GridBagConstraints();
		gbc_lblQuantities.gridwidth = 11;
		gbc_lblQuantities.insets = new Insets(0, 0, 5, 5);
		gbc_lblQuantities.gridx = 1;
		gbc_lblQuantities.gridy = 8;
		contentPane.add(lblQuantities, gbc_lblQuantities);
		
	}
	
	private void initTables() {
		
		qtyTable = new JTable();
		qtyTable.setFillsViewportHeight(true);
		
		GridBagConstraints gbc_qtyTable = new GridBagConstraints();
		gbc_qtyTable.insets = new Insets(0, 0, 5, 5);
		gbc_qtyTable.gridheight = 3;
		gbc_qtyTable.gridwidth = 11;
		gbc_qtyTable.fill = GridBagConstraints.BOTH;
		gbc_qtyTable.gridx = 1;
		gbc_qtyTable.gridy = 9;
		
		DefaultTableModel qtyTableM = (DefaultTableModel)qtyTable.getModel();
		
		qtyTableM.setColumnIdentifiers(new String[]{"Qty","Type","Location","Purchase"});
		
		qtyScrollPane = new JScrollPane(qtyTable);
		contentPane.add(qtyScrollPane, gbc_qtyTable);
		
		adoptTable = new JTable();
		adoptTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		adoptTable.setFillsViewportHeight(true);
		
		GridBagConstraints gbc_adoptScrollPane = new GridBagConstraints();
		gbc_adoptScrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_adoptScrollPane.gridheight = 3;
		gbc_adoptScrollPane.gridwidth = 11;
		gbc_adoptScrollPane.fill = GridBagConstraints.BOTH;
		gbc_adoptScrollPane.gridx = 1;
		gbc_adoptScrollPane.gridy = 5;
		
		DefaultTableModel adoptTableM = (DefaultTableModel)adoptTable.getModel();
		
		adoptTableM.setColumnIdentifiers(new String[]{"Dept","Course","Section"});
		
		adoptScrollPane = new JScrollPane(adoptTable);
		contentPane.add(adoptScrollPane, gbc_adoptScrollPane);
		
	}
	
	private void initButtons() {
		
		btnMassUpdateFrom = new JButton("Mass Update from MBS");
		btnMassUpdateFrom.setActionCommand("Mass Update");
		btnMassUpdateFrom.addActionListener(new InvActionListener());
		GridBagConstraints gbc_btnMassUpdateFrom = new GridBagConstraints();
		gbc_btnMassUpdateFrom.anchor = GridBagConstraints.WEST;
		gbc_btnMassUpdateFrom.gridwidth = 9;
		gbc_btnMassUpdateFrom.insets = new Insets(0, 0, 0, 5);
		gbc_btnMassUpdateFrom.gridx = 1;
		gbc_btnMassUpdateFrom.gridy = 12;
		contentPane.add(btnMassUpdateFrom, gbc_btnMassUpdateFrom);
		
	}
	
	private class InvActionListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("Mass Update")) {
				MBSUpdate mu = new MBSUpdate();
				mu.setVisible(true);
			}
		}
	}
	
	private class InvKeyListener extends KeyAdapter {
		
		@Override
		public void keyPressed(KeyEvent arg0) {
			if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
				Util.printStatusString("You Pressed Enter!");
				
				emptyTables();
				emptyEds();
				
				String isbn = invNumField.getText();
				Book b = inv.getBook(isbn);
				
				if (b == null && !isbn.equals("")) {
					Util.printStatusString("isbn: "+isbn);
					try {
						b = inv.getBook(isbn);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
				updateLabels(b);
				
				if (b == null) {
					Util.printStatusString("no book entered");
					noQty();
				}
				else {
					QuantityList ql = inv.qtyLookup(isbn);
					Util.printStatusString("QuantityList for isbn: "+isbn);
					
					if (ql == null || ql.size() == 0) {
						Util.printStatusString("No quantities.");
						noQty();
					}
					else
						for (int i = 0; i < ql.size(); ++i)
							displayQuantity(ql.get(i));
				}
				((DefaultTableModel)qtyTable.getModel()).fireTableDataChanged();
			}
		}
		
		private void updateLabels(Book b) {
			if (b == null) {
				lblAuthor.setText("Author");
				lblTitle.setText("Title");
				jcbEd.addItem("Ed");
			}
			else {
				lblAuthor.setText(b.getAuthor());
				lblTitle.setText(b.getTitle());
				for (Edition e : b.getEditions())
					jcbEd.addItem(e.getEd());
			}
		}
		
		private void emptyEds() {
			jcbEd.removeAllItems();
		}
		
		private void emptyTables() {
			emptyTable((DefaultTableModel)qtyTable.getModel());
			emptyTable((DefaultTableModel)adoptTable.getModel());
		}
		
		private void emptyTable(DefaultTableModel dtm) {
			for (int i = 0; i < dtm.getRowCount(); )
				dtm.removeRow(i);
		}
		
		private void displayQuantity(Quantity q) {
			
			String[] tmp = new String[]{
					String.valueOf(q.getQty()),
					q.getType().getType(),
					q.getType().getLocation().toString(),
					String.format("$%1$.2f",q.getType().getPrice())};
			((DefaultTableModel)qtyTable.getModel()).addRow(tmp);
			
		}
		
		private void noQty() {
			((DefaultTableModel)qtyTable.getModel()).addRow(new String[]{"","None"});
		}
		
	}

}
